﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using AGDN;
using Microsoft.Xna.Framework.Graphics;

namespace Sudum
{
    class MenuOption
    {
        #region Properties

        public FlowAction Action { get; set; }
        public string Text { get; set; }
        public Color NormalColor { get; set; }
        public Color SelectedColor { get; set; }
        public string Font { get; set; }
        public int FontSize { get; set; }

        #endregion

        #region constructors

        public MenuOption(string text, FlowAction action)
        {
            this.Action = action;
            this.Text = text;
            this.NormalColor = Color.White;
            this.SelectedColor = Color.Yellow;
            this.Font = "Tahoma";
            this.FontSize = 20;
        }

        #endregion

        #region methods

        public void Update()
        {
        }

        public void Render(XnaArabicTextRender renderer, bool selected, Vector2 position)
        {
            if (!selected)
                renderer.DrawString(Text, Font, FontSize, NormalColor, position);
            else renderer.DrawString(Text, Font, FontSize + 2, SelectedColor, position);
        }

        public Point Dimensions
        {
            get { return XnaArabicTextRender.MeasureText(Text, Font, FontSize); }
        }
        #endregion
    }

    abstract class GameState_BaseGameMenu : IGameState
    {
        #region Fields

        XnaArabicTextRender textRender = new XnaArabicTextRender(Sudum.Me.GraphicsDevice);
        protected List<MenuOption> menuOptions = new List<MenuOption>();
        protected int selectedOption = 0;
        protected int verticalIndent = 1;
        protected int posMenuX;
        protected int posMenuY;

        #endregion

        #region Properties

        protected List<MenuOption> MenuOptions
        {
            get { return menuOptions; }
        }
        public int VerticalIndent
        {
            get { return verticalIndent; }
            set
            {
                if (value >= 0)
                    verticalIndent = value;
                else
                    verticalIndent = 0;
            }
        }

        #endregion

        #region methods

        public FlowAction Update()
        {
			if (InputManager.Me.Pressed(Action.Up))
            {
                if (selectedOption > 0)
                    selectedOption--;
                else selectedOption = menuOptions.Count - 1;
                return FlowAction.FlowTo_Continue;
            }

			if (InputManager.Me.Pressed(Action.Down))
            {
                if (selectedOption < menuOptions.Count -1)
                    selectedOption++;
                else selectedOption = 0;
                return FlowAction.FlowTo_Continue;
            }

			if (InputManager.Me.Pressed(Action.Select) || InputManager.Me.Pressed(Action.Fire))
                return menuOptions[selectedOption].Action;

			if (InputManager.Me.Pressed(Action.Exit))
                return FlowAction.FlowTo_Exit;

            return FlowAction.FlowTo_Continue;
        }

        public void Render()
        {
            Sudum.Me.GraphicsDevice.Clear(Color.Black);
            for (int i = 0; i < menuOptions.Count; i++)
            {
                bool selected = (selectedOption == i);
                Point size = menuOptions[i].Dimensions;
                float x = posMenuX - size.X;
                float y = posMenuY + i * (size.Y + verticalIndent);
                menuOptions[i].Render(textRender, selected, new Vector2(x, y));
            }
        }

        #endregion
    }

    class GameState_PrincipalGameMenu : GameState_BaseGameMenu
    {
        public GameState_PrincipalGameMenu()
        {
            base.verticalIndent = 5;
            base.selectedOption = 1;
            base.posMenuX = 450;
            base.posMenuY = 150;

            menuOptions.Add(new MenuOption("القصة", FlowAction.FlowTo_Continue));
            menuOptions.Add(new MenuOption("لعبة جديدة", FlowAction.FlowTo_Play));
            menuOptions.Add(new MenuOption("إختيار مرحلة", FlowAction.FlowTo_Continue));
            menuOptions.Add(new MenuOption("تحرير مرحلة", FlowAction.FlowTo_Continue));
            menuOptions.Add(new MenuOption("فريق التطوير", FlowAction.FlowTo_Continue));
            menuOptions.Add(new MenuOption("مساعدة", FlowAction.FlowTo_Continue));
            menuOptions.Add(new MenuOption("خروج", FlowAction.FlowTo_Exit));
        }
    }
}
